# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - patch for chromium GN
#  - patch for chromium skia
#  - patch for chromium to run blink bindings generation single threaded
#  - build v7 with -j4

highmem=1

pkgname=qt5-webengine
_basever=5.15.14
pkgver=5.15.17
pkgrel=6
arch=('x86_64')
url='https://www.qt.io'
license=('LGPL3' 'LGPL2.1' 'BSD')
pkgdesc='Provides support for web applications using the Chromium browser project'
depends=('qt5-webchannel' 'qt5-location' 'libxcomposite' 'libxrandr' 'pciutils' 'libxss' 'libxkbfile' 'libxdamage'
         'libevent' 'snappy' 'nss' 'libxslt' 'minizip' 'ffmpeg' 'libvpx' 'libxtst' 'ttf-font')
makedepends=('git' 'python' 'gperf' 'jsoncpp' 'ninja' 'qt5-tools' 'poppler' 'pipewire' 'nodejs' 'python-html5lib')
optdepends=('pipewire: WebRTC desktop sharing under Wayland')
groups=('qt5')
_pkgfqn=${pkgname/5-/}
source=(kde-$_pkgfqn::git+https://code.qt.io/qt/qtwebengine.git#tag=v${pkgver}-lts
        git+https://code.qt.io/qt/qtwebengine-chromium.git
        qt5-webengine-ffmpeg5.patch
        qt5-webengine-ffmpeg7.patch
        qt5-webengine-pipewire-0.3.patch
        qt5-webengine-icu-75.patch
        qt5-webengine-ninja-1.12.patch
        python3.12-imp.patch
        python3.12-six.patch
        0001-ARM-toolchain-fixes.patch
        0002-Fix-ARM-skia-ICE.patch
        0003-bind-gen-Support-single_process-flag-in-generate_bin.patch
        0004-Run-blink-bindings-generation-single-threaded.patch)
sha256sums=('c2488e6d6778cf9954fcc45e94acb762376581a99df7f889f13a93cefd356940'
            'SKIP'
            'c50d3019626183e753c53a997dc8a55938847543aa3178d4c51f377be741c693'
            '82a56cfc3e278617e11093078abe672a73c6fdaad98a2c8604ad8f22bc879ce5'
            '5e3a3c4711d964d5152a04059a2b5c1d14bb13dd29bce370120f60e85b476b6f'
            '7cac28ba784d24b4abf6414079548ada165343af507ecd8e23cbe7e4f63ae52f'
            '6672741b64d896dc555c8ee42ca2329c4f20d5f406095a69fe72da44b3a142f4'
            '1f4357708e985bb5aca61a7e5dc4c0c1285d2af00994bb49ff89ede78198e0d2'
            'ac87ec55ee5cbcf2d520e1ea433d041c0bf754271a17f859edbb9976f192ce3f'
            'abccb5349ec341fd912be93d055f0cdf6becfdb5fa3312769777f750cfd4351a'
            'cad41451dffb5cc67c57882526c6c19795e6e49e7b704f92ec76dd3499486654'
            '4e58aca7f71037f154bbdfd03deb671462da433c241fd6429ca6adf2c7738f75'
            'e24537ac22f84daa5ff067abf9c3421897df9cd60d2bcd0809ff8c10e4ab0efa')

prepare() {
  mkdir -p build

  cd kde-$_pkgfqn
  git submodule init
  git submodule set-url src/3rdparty "$srcdir"/qtwebengine-chromium
  git submodule set-branch --branch 87-based src/3rdparty
  git -c protocol.file.allow=always submodule update

  patch -p1 -d src/3rdparty -i "$srcdir"/qt5-webengine-ffmpeg5.patch # Fix build with ffmpeg 5
  patch -p1 -d src/3rdparty -i "$srcdir"/qt5-webengine-pipewire-0.3.patch # Port to pipewire 0.3
  patch -p2 -d src/3rdparty/chromium -i "$srcdir"/qt5-webengine-icu-75.patch # Fix build with ICU 75
  patch -p2 -d src/3rdparty/chromium -i "$srcdir"/qt5-webengine-ninja-1.12.patch # Fix build with ninja 1.12
  patch -p1 -d src/3rdparty/chromium -i "$srcdir"/python3.12-imp.patch # Fix build with python 3.12 - patch from Debian
  patch -p1 -d src/3rdparty/chromium -i "$srcdir"/python3.12-six.patch # Fix build with python 3.12 - patch from Debian
# Fix build with ffmpeg 7 - Chromium patches
  patch -p1 -d src/3rdparty/chromium -i "$srcdir"/qt5-webengine-ffmpeg7.patch

  [[ $CARCH == "armv7h" ]] && export NINJAJOBS="-j1"
  cd "$srcdir/kde-$_pkgfqn/src/3rdparty"
  patch -p1 -i ${srcdir}/0001-ARM-toolchain-fixes.patch
  patch -p1 -i ${srcdir}/0002-Fix-ARM-skia-ICE.patch
  patch -p1 -i ${srcdir}/0003-bind-gen-Support-single_process-flag-in-generate_bin.patch
  patch -p1 -i ${srcdir}/0004-Run-blink-bindings-generation-single-threaded.patch
}

build() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  cd build
  qmake ../kde-$_pkgfqn -- \
    -proprietary-codecs \
    -system-ffmpeg \
    -webp \
    -webengine-icu \
    -spellchecker \
    -webengine-kerberos \
    -webengine-webrtc-pipewire \
    -webengine-python-version python3
  make
}

package() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  cd build
  make INSTALL_ROOT="$pkgdir" install

  # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
  find "$pkgdir/usr/lib" -type f -name '*.prl' \
    -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  install -Dm644 "$srcdir"/kde-$_pkgfqn/src/3rdparty/chromium/LICENSE "$pkgdir"/usr/share/licenses/${pkgname}/LICENSE.chromium

  # Fix cmake dependency versions
  sed -e "s|$pkgver\ |$_basever |" -i "$pkgdir"/usr/lib/cmake/*/*Config.cmake
}
